package com.ihr360.expr.function.excel;


import com.ihr360.expr.ExprError;
import com.ihr360.expr.ExprException;
import com.ihr360.expr.ExprString;
import com.ihr360.expr.context.IEvaluationContext;
import com.ihr360.expr.core.AbstractFunction;
import com.ihr360.expr.core.Expr;
import com.ihr360.expr.core.ExprNumber;
import com.ihr360.expr.utils.ExprUtils;
import com.ihr360.expr.utils.ValueFormatter;

/**
 * 没有实现
 */
public class TEXT extends AbstractFunction
{
    @Override
    public Expr evaluate(IEvaluationContext context, Expr[] args)
            throws ExprException {
        assertArgCount(args, 2);

        Expr eV = ExprUtils.evalArg(context, args[0]);
        if (eV instanceof ExprError){
            return eV;
        }
        if (!isNumber(eV)){
            return ExprError.VALUE;
        }
        double value = ((ExprNumber) eV).doubleValue();

        Expr eF = ExprUtils.evalArg(context, args[0]);
        if (eF instanceof ExprError){
            return eF;
        }
        String s = asString(context, eF, false);

        String res = ValueFormatter.format(value, s);
        if (res != null)
            return new ExprString(res);
        else
            return ExprError.VALUE;
    }
}
